<template>
  <page-wrap back hide-tab title="面试通知" class="page" stayStill>
    <view class="tab">
      <u-tabs-swiper
        ref="tabs"
        :list="tabs"
        :is-scroll="false"
        :current="current"
        :active-color="activeColor"
        :inactive-color="inactiveColor"
        font-size="28"
        height="104"
        bar-width="56"
        bar-height="14"
        :bar-style="barStyleObj"
        @change="handleTab"
      ></u-tabs-swiper>
    </view>
    <swiper
      class="swiper-box"
      :style="
        'height: calc(100vh - ' +
        navHeight +
        'px - 82rpx - env(safe-area-inset-bottom));'
      "
      :duration="400"
      :current="swiperCurrent"
      @transition="handleTransition"
      @animationfinish="handleAnimation"
    >
      <block v-for="(item, index) in list" :key="index">
        <swiper-item>
          <scroll-view scroll-y class="scroll-views" @scrolltolower="loadMore">
            <interview-notice-list
              :list="item.list"
              :status="item.loading"
              :show-empty="item.showEmpty"
              @item-click="handleNoticeItem"
            ></interview-notice-list>
          </scroll-view>
        </swiper-item>
      </block>
    </swiper>
  </page-wrap>
</template>

<script>
import InterviewNoticeList from "./InterviewNoticeList";
import { getPInterviewList } from "@/api/index";
import { getCInterviewList } from "@/api/modules/company";
// import ShareMixin from '@/mixins/share';

export default {
  // mixins: [ShareMixin],
  components: { InterviewNoticeList },
  data() {
    return {
      activeColor: '#333333',
      inactiveColor: "#666666",
      navHeight: 0,
      personId: uni.getStorageSync("person-id").slice(2) || undefined,
      companyId: uni.getStorageSync("company-id"),
      isCompany: uni.getStorageSync("login-status") == "company",
      current: 0,
      swiperCurrent: 0,
      emptyShow: true,
      tabs: [
        {
          name: "全部面试",
        },
        {
          name: "即将面试",
        },
        {
          name: "已面试",
        },
      ],
      list: [
        {
          type: "qbms",
          page: 0,
          list: [],
          loading: "loadmore",
          showEmpty: false,
        },
        {
          type: "jjms",
          page: 0,
          list: [],
          loading: "loadmore",
          showEmpty: false,
        },
        {
          type: "yms",
          page: 0,
          list: [],
          loading: "loadmore",
          showEmpty: false,
        },
      ],
      barStyleObj: {
        bottom: '16rpx',
        backgroundColor: "transparent",
        backgroundImage: `url(${this.$imgBaseSelf}/select.png)`,
        backgroundSize: "100% 100%",
        borderRadius: '0',
      },
      init: false
    };
  },
  created() {
    // #ifdef MP-WEIXIN
    this.navHeight = getApp().globalData.navHeight;
    // #endif
  },
  mounted() {
    this.getData(true);
  },
  onShow() {
    var pages = getCurrentPages();    // 获取当前页面栈
    var curPage = pages[pages.length - 1];    // 当前页面
    if(curPage && curPage.data && curPage.data.init){
        curPage.data.init = false;
        this.getData(true);
    }
  },
  // onShareAppMessage() {
  //   return {
  //     title: this.$selfName,
  //     path: "/pages/index/index",
  //   };
  // },
  // onShareTimeline() {
  //   return {
  //     title: this.$selfName,
  //     path: "/pages/index/index",
  //   };
  // },
  methods: {
    getData(init) {
      let currentSelectItem = this.list[this.swiperCurrent];
      if(init) {
        currentSelectItem.list = [];
        currentSelectItem.page = 0;
        currentSelectItem.loading = 'loadmore';
      }
      let loading = currentSelectItem.loading;
      let type = currentSelectItem.type;
      let page = currentSelectItem.page;
      if (loading == "loading" || loading == "nomore") return;
      this.$set(currentSelectItem, "loading", "loading");
      let params = {
        person_id: this.personId,
        type: type,
        page: page,
        page_size: "20",
      };
      let fn = getPInterviewList;
      // 企业端
      if(this.isCompany) {
        fn = getCInterviewList;
        params = {
          company_id: this.companyId,
          synergy_id: 0,
          type: type,
          conditionArr: JSON.stringify({
            page: page,
            page_size: "20",
            person_id: this.personId,
          }),    
        }
      }
      fn(params).then((res) => {
        // res.data = this.generator();
        if (res.data) {
          let list = currentSelectItem.list;
          res.data.forEach(item => {
            if(this.isCompany) {
              item.title = item.person_name + ' · ' + item.zw_name;
              item.logo = item.person_photo;
            } else {
              item.title = item.company_name;
              item.logo = item.company_logo;
            }
          })
          list.push(...res.data);
          this.$set(currentSelectItem, "list", list);
          this.$set(currentSelectItem, "loading", "loadmore");
          if (list.length >= res.pageparam.sums) {
            this.$set(currentSelectItem, "loading", "nomore");
            return;
          }
        } else {
          this.$set(currentSelectItem, "loading", "nomore");
          this.$set(currentSelectItem, "showEmpty", true);
        }
      });
    },
    handleTab(index) {
      this.swiperCurrent = index;
    },
    handleTransition(e) {
      let dx = e.detail.dx;
      this.$refs.tabs.setDx(dx);
    },
    handleAnimation(e) {
      let current = e.detail.current;
      this.$refs.tabs.setFinishCurrent(current);
      this.swiperCurrent = current;
      this.current = current;
      if (!this.list[this.swiperCurrent].list.length) {
        this.getData();
      }
    },
    handleNoticeItem(item, index) {
      console.log(item, index);
      uni.navigateTo({
        url: '/pagesSub/public/interview/index?id=' + item.pmail_id,
      });
    },
    loadMore() {
      this.list[this.swiperCurrent].page += 1;
      this.getData();
    },
    generator() {
      let obj = {
        idate: "8月20日",
        status: 0,
        logopath: "",
        jtzw: "职位名称",
        cate: "行业类型",
        cxz: "民营企业",
        yuangong: "500-999",
      };
      let tempList = [];
      for (let i = 0; i < 10; i++) {
        let temObj = Object.assign({}, obj, { status: i });
        tempList.push(temObj);
      }
      return tempList;
    },
  },
};
</script>

<style lang="scss">
.page {
  display: flex;
  flex-direction: column;
  .tab {
    border-bottom: 1px solid #ececec;
  }

  .swiper-box {
    display: flex;
    flex-direction: column;
    ::v-deep .uni-swiper-wrapper,
    ::v-deep swiper-item {
      flex: 1;
      height: 1rpx;
    }
    ::v-deep .u-empty {
      position: absolute;
      left: 50%;
      top: 48%;
      transform: translate(-50%, -50%);
    }
  }

  .scroll-views {
    height: 100%;
  }
}
</style>
